package com.huangge1199.codewars;

import java.util.Arrays;

/**
 * @ClassName Solution
 * @Description TODO
 * @Author huangge1199
 * @Date 2021/10/8 15:37
 **/
class rangeExtraction {
    public static String solution(int[] arr) {
        Arrays.sort(arr);
        String str = "";
        int bef = arr[0];
        int begin = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == bef + 1) {
                bef = arr[i];
            } else if (bef - begin > 1) {
                str += begin + "-" + bef + ",";
                begin = bef = arr[i];
            } else {
                for (int j = begin; j <= bef; j++) {
                    str += j + ",";
                }
                begin = bef = arr[i];
            }
        }
        if (bef - begin > 1) {
            str += begin + "-" + bef;
        } else {
            int j = begin;
            for (; j < bef; j++) {
                str += j + ",";
            }
            str += j;
        }
        return str;
    }
}
